package com.baidu.lenovo.autotest;

import android.app.Notification;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Intent;
import android.content.res.AssetFileDescriptor;
import android.content.res.AssetManager;
import android.media.MediaPlayer;
import android.os.IBinder;
import android.util.Log;

import com.baidu.aip.asrwakeup3.core.util.FileUtil;

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import static com.baidu.lenovo.autotest.ExcelUtil.getXlsData;


public class MyService extends Service {
    String message = "";
    int shortTime = 2500;
    int longTime=20000;
    int repeat=100;
    private boolean running = true;


    public MyService() {
    }




    private void initMediaPlayer(MediaPlayer m, String s) {

        try {

            AssetManager assetManager;
            assetManager = getAssets();
            AssetFileDescriptor fileDescriptor = null;
            m.setDataSource(s);
//            fileDescriptor = assetManager.openFd(s);
//            m.setDataSource(fileDescriptor.getFileDescriptor(), fileDescriptor.getStartOffset(), fileDescriptor.getStartOffset());
            m.prepare(); // 让MediaPlayer进入到准备状态
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    public void save(String s,String file) {
        File ttsFile;
        BufferedOutputStream ttsFileBufferedOutputStream;
        String tmpDir = FileUtil.createTmpDir(this);
        String filename = file+ ".txt";
        // 保存的语音文件是 16K采样率 16bits编码 单声道 pcm文件。
        ttsFile = new File(tmpDir, filename);
        Log.i("zxx", "try to write audio file to " + ttsFile.getAbsolutePath());
        try {
            if (ttsFile.exists()) {
                ttsFile.delete();
            }
            ttsFile.createNewFile();
            // 创建FileOutputStream对象
            FileOutputStream ttsFileOutputStream = new FileOutputStream(ttsFile);
            // 创建BufferedOutputStream对象
            ttsFileBufferedOutputStream = new BufferedOutputStream(ttsFileOutputStream);
            ttsFileBufferedOutputStream.write(s.getBytes());
            ttsFileBufferedOutputStream.flush();
            ttsFileBufferedOutputStream.close();

        } catch (IOException e) {
            // 请自行做错误处理
            e.printStackTrace();
            throw new RuntimeException(e);
        }
    }
    @Override
    public IBinder onBind(Intent intent) {
        return new Binder();
    }

    /**
     *创建一个类继承Binder,来进行
     */
    public class Binder extends android.os.Binder{
        public void setData(int data[]){
            MyService.this.longTime = data[0];
            MyService.this.repeat = data[1];
        }
    }



    @Override
    public void onCreate() {
//        running = true;
        new Thread(){
            @Override
            public void run() {
                super.run();
                while (running) {

                    MediaPlayer mediaPlayer1 = new MediaPlayer();
                    MediaPlayer mediaPlayer2 = new MediaPlayer();
                    MediaPlayer mediaPlayer3 = new MediaPlayer();
                    MediaPlayer mediaPlayer4 = new MediaPlayer();
                    MediaPlayer mediaPlayer5 = new MediaPlayer();
                    MediaPlayer mediaPlayer = new MediaPlayer();

                        List excel = new ArrayList();
                    try {
                        excel = getXlsData("/sdcard/answer.xls");
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                    String[] mediaName = new String[excel.size()];
                        Answer mAnswer;
                        for (int row = 0; row < excel.size(); row++) {
                            mAnswer = (Answer) excel.get(row);
                            mediaName[row] = "/sdcard/" + mAnswer.name;
                            Log.i("name", mAnswer.name);
                        }
                        initMediaPlayer(mediaPlayer, "/sdcard/q00000001_s001895_near_q001.wav");
                        initMediaPlayer(mediaPlayer1, mediaName[0]);
                        initMediaPlayer(mediaPlayer2, mediaName[1]);
                        initMediaPlayer(mediaPlayer3, mediaName[2]);
                        initMediaPlayer(mediaPlayer4, mediaName[3]);
                        initMediaPlayer(mediaPlayer5, mediaName[4]);

                        for (int i = 0; i <repeat; i++) {
                            try {
                                Thread.sleep(longTime);

                            mediaPlayer.start();
                                message += printLog(message, "小度小度");
                                Thread.sleep(shortTime);
//                            play(Integer.toString(i)+".wav");
//                            play("q00049210.wav");
                                mediaPlayer1.start();
                                Answer myAnswer = (Answer) excel.get(0);
//                            message+=printLog(message,.);
                                message += printLog(message, myAnswer.question);
                                Thread.sleep(shortTime);
                                message += printLog(message, myAnswer.reply);
                                Thread.sleep(longTime);
//                            stop(mediaPlayer1);

                                mediaPlayer.start();
                                message += printLog(message, "小度小度");
                                Thread.sleep(shortTime);
//                            play(Integer.toString(i)+".wav");
//                            play("q00049210.wav");
                                mediaPlayer2.start();
                                myAnswer = (Answer) excel.get(1);
//                            message+=printLog(message,.);
                                message += printLog(message, myAnswer.question);
                                Thread.sleep(shortTime);
                                message += printLog(message, myAnswer.reply);
                                Thread.sleep(longTime);
//                            stop(mediaPlayer2);


                                mediaPlayer.start();
                                message += printLog(message, "小度小度");
                                Thread.sleep(shortTime);
////                            play(Integer.toString(i)+".wav");
////                            play("q00049210.wav");
                                mediaPlayer3.start();
                                myAnswer = (Answer) excel.get(2);
//                            message+=printLog(message,.);
                                message += printLog(message, myAnswer.question);
                                Thread.sleep(shortTime);
                                message += printLog(message, myAnswer.reply);
                                Thread.sleep(longTime);
//                            stop(mediaPlayer3);


                                mediaPlayer.start();
                                message += printLog(message, "小度小度");
                                Thread.sleep(shortTime);
//                            play(Integer.toString(i)+".wav");
//                            play("q00049210.wav");
                                mediaPlayer4.start();
                                myAnswer = (Answer) excel.get(3);
//                            message+=printLog(message,.);
                                message += printLog(message, myAnswer.question);
                                Thread.sleep(shortTime);
                                message += printLog(message, myAnswer.reply);
                                Thread.sleep(longTime);
//                            stop(mediaPlayer4);


                                mediaPlayer.start();
                                message += printLog(message, "小度小度");
                                Thread.sleep(shortTime);
//                            play(Integer.toString(i)+".wav");
//                            play("q00049210.wav");
                                mediaPlayer5.start();
                                myAnswer = (Answer) excel.get(4);
//                            message+=printLog(message,.);
                                message += printLog(message, myAnswer.question);
                                Thread.sleep(shortTime);
                                message += printLog(message, myAnswer.reply);
                                Thread.sleep(longTime);
//                            stop(mediaPlayer5);
                                save(message, "MusicPlayer");
                                if(i==repeat-1) {
                                    running = false;
                                }

                            } catch (InterruptedException e) {
                                e.printStackTrace();
                            }


                    }


                }

            }
        }.start();
        super.onCreate();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        running = false;
    }
    private String printLog(String m,String text) {

        m =text+ "  ;time=" + System.currentTimeMillis();
        m += "\n";
        Log.i(getClass().getName(), text);
        return m;

    }

}
